﻿using System;
using UnityEngine;

namespace MGFramework
{
    public class AndroidTools
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        private AndroidJavaObject javaObject = null;

        public AndroidTools(){
            javaObject = new AndroidJavaObject("cc.ideawedo.lframework.Tools");
            javaObject.Call("OnInit");
        }
#endif

        /// <summary>
        /// 显示开屏
        /// </summary>
        /// <param name="p_name">图片的名字</param>
        public void ShowSplash(string p_name)
        {
            object[] data = new object[1];
            data[0] = p_name;
            CallAndroid("ShowSplash",data);
        }

        /// <summary>
        /// 隐藏开屏
        /// </summary>
        public void HideSplash()
        {
            CallAndroid("HideSplash");
        }

        /// <summary>
        /// 安卓震动
        /// </summary>
        /// <param name="time">震动时间，单位:毫秒</param>
        public void Shake(int time)
        {
            object[] data = new object[1];
            data[0] = time;
            CallAndroid("Shake", data);
        }

        /// <summary>
        /// Unity原生震动 并没什么用，只是为了让Unity自动添加震动权限
        /// </summary>
        public void vibrate() {
            Handheld.Vibrate();
        }

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="msg">提示框内容</param>
        public void ShowToast(string msg) {
            object[] data = new object[1];
            data[0] = msg;
            CallAndroid("ShowToast", data);
        }

        /// <summary>
        /// 安卓原生打印（可以自定义标签，方便查找）
        /// </summary>
        /// <param name="tag">标签</param>
        /// <param name="msg">内容</param>
        public void AndroidDebug(string tag,string msg) {
            object[] data = new object[2];
            data[0] = tag;
            data[1] = msg;
            CallAndroid("AndroidDebug", data);
#if UNITY_EDITOR
            Debug.Log(tag + ":" + msg);
#endif
        }

        /// <summary>
        /// 调用安卓方法
        /// </summary>
        /// <param name="method">方法名</param>
        /// <param name="data">参数</param>
        private void CallAndroid(string method, object[] data = null)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            try
            {
                
                if (data == null)
                {
                    javaObject.Call(method);
                }
                else
                {
                    javaObject.Call(method, data);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
#endif
        }
    }
}
